package gui.newgui.startupwindow;

import gui.newgui.mainwindow.MainWindow;
import gui.newgui.newtournamentwindow.NewTournamentWindow2;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.LayoutStyle;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import javax.swing.border.LineBorder;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

import core.Constants;
import core.CustomIcons;
import core.GuiComponentLabels;

public class StartUpWindow extends javax.swing.JFrame {
	private JPanel logoContainer;
	private JPanel buttonContainer;
	private JButton newTournamentButton;
	private JButton closeButton;
	private JButton openTournamentButton;
	private JLabel logoLabel;

	/**
	 * Auto-generated main method to display this JFrame
	 */
	public static void main(String [] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				StartUpWindow inst = new StartUpWindow();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}

	public StartUpWindow() {
		super();
		this.setUndecorated(true);
		this.initGUI();
	}

	private void initGUI() {
		try {
			GroupLayout thisLayout = new GroupLayout(this.getContentPane());
			this.getContentPane().setLayout(thisLayout);
			this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			{
				this.logoContainer = new JPanel();
				BorderLayout logoContainerLayout = new BorderLayout();
				this.logoContainer.setLayout(logoContainerLayout);
				{
					this.logoLabel = new JLabel();
					this.logoLabel.setIcon(CustomIcons.logoBig);
					this.logoContainer.add(this.logoLabel, BorderLayout.CENTER);
					this.logoLabel.setPreferredSize(new java.awt.Dimension(612, 321));
				}
			}
			{
				this.buttonContainer = new JPanel();
				GroupLayout buttonContainerLayout = new GroupLayout(this.buttonContainer);
				this.buttonContainer.setLayout(buttonContainerLayout);
				this.buttonContainer.setBorder(new LineBorder(new java.awt.Color(0, 0, 0), 1, false));
				{
					this.newTournamentButton = new JButton();
					this.newTournamentButton.setIcon(CustomIcons.newFileIcon);
					this.newTournamentButton.setText(GuiComponentLabels.NEW_TOURNAMENT);
					this.newTournamentButton.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							SwingUtilities.invokeLater(new Runnable() {
								public void run() {
									NewTournamentWindow2 inst = new NewTournamentWindow2(StartUpWindow.this);
									inst.setLocationRelativeTo(null);
									inst.setVisible(true);
								}
							});
						}
					});
				}
				{
					this.openTournamentButton = new JButton();
					this.openTournamentButton.setIcon(CustomIcons.openFolderIcon);
					this.openTournamentButton.setText(GuiComponentLabels.OPEN_BUTTON);
					this.openTournamentButton.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							boolean isChosen = false;

							final JFileChooser chooser = new JFileChooser();
							final FileFilter filter = new FileNameExtensionFilter("xml Dateien", Constants.XML_FILEENDING);
							chooser.addChoosableFileFilter(filter);
							final int ret = chooser.showDialog(null, GuiComponentLabels.OPEN);

							if (ret == JFileChooser.APPROVE_OPTION) {
								final File fileToOpen = chooser.getSelectedFile();

								/** open the main Window */
								SwingUtilities.invokeLater(new Runnable() {
									public void run() {
										MainWindow inst;
										inst = new MainWindow(fileToOpen);
										inst.setLocationRelativeTo(null);
										inst.setVisible(true);
									}
								});
								isChosen = true;
							}

							if (isChosen) {
								StartUpWindow.this.dispose();
							}
						}
					});
				}
				{
					this.closeButton = new JButton();
					this.closeButton.setIcon(CustomIcons.closeIcon);
					this.closeButton.setText(GuiComponentLabels.CLOSE_BUTTON);
					this.closeButton.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							StartUpWindow.this.dispose();
						}
					});
				}
				buttonContainerLayout.setHorizontalGroup(buttonContainerLayout.createSequentialGroup().addContainerGap()
						.addComponent(this.newTournamentButton, GroupLayout.PREFERRED_SIZE, 200, GroupLayout.PREFERRED_SIZE).addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
						.addComponent(this.openTournamentButton, 0, 215, Short.MAX_VALUE).addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED, 1, GroupLayout.PREFERRED_SIZE)
						.addComponent(this.closeButton, GroupLayout.PREFERRED_SIZE, 200, GroupLayout.PREFERRED_SIZE).addContainerGap());
				buttonContainerLayout.setVerticalGroup(buttonContainerLayout
						.createSequentialGroup()
						.addContainerGap()
						.addGroup(
								buttonContainerLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
										.addComponent(this.openTournamentButton, GroupLayout.Alignment.BASELINE, 0, 42, Short.MAX_VALUE)
										.addComponent(this.newTournamentButton, GroupLayout.Alignment.BASELINE, 0, 42, Short.MAX_VALUE)
										.addComponent(this.closeButton, GroupLayout.Alignment.BASELINE, 0, 42, Short.MAX_VALUE)).addContainerGap());
			}
			thisLayout.setVerticalGroup(thisLayout.createSequentialGroup().addContainerGap().addComponent(this.logoContainer, GroupLayout.PREFERRED_SIZE, 321, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED).addComponent(this.buttonContainer, 0, 94, Short.MAX_VALUE).addContainerGap());
			thisLayout.setHorizontalGroup(thisLayout
					.createSequentialGroup()
					.addContainerGap()
					.addGroup(
							thisLayout.createParallelGroup().addComponent(this.logoContainer, GroupLayout.Alignment.LEADING, 0, 610, Short.MAX_VALUE)
									.addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup().addComponent(this.buttonContainer, 0, 598, Short.MAX_VALUE).addGap(12))));
			this.pack();
			this.setSize(686, 424);
		} catch (Exception e) {
			// add your error handling code here
			e.printStackTrace();
		}
	}

}
